package com.ym.controller.open;


import com.alibaba.fastjson.JSONArray;
import com.ym.common.utils.SourceFileUtil;
import com.ym.core.config.ConfigCenterProperties;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.medical.service.IMessageCodeSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 指定开放的API
 *
 * @author: 李涛
 * @version: 2019年11月18日 10:03
 */
@Api(tags = "open-指定开放的API")
@RestController
@Validated
@RequestMapping("/open/api/")
public class OpenApiController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(OpenApiController.class);


	@Autowired
	private IMessageCodeSV messageCodeSV;

	@Autowired
	private RestTemplate restTemplate;

	@Autowired
	private ConfigCenterProperties centerProperties;


	@ApiOperation("pc后台管理系统首页验证码发送")
	@GetMapping(value = "/pc/code")
	public APIResponse sendCode(
			@ApiParam(name = "mobile", value = "用户手机号码", required = true)
			@RequestParam String mobile) throws Exception {
		Map<String, Object> param = new HashMap<>();
		param.put("mobile", mobile);
		String url = centerProperties.getAuthSystemApi()
				.getMobileCode();
		String appKey = SourceFileUtil.source("authKey.txt");
		HttpHeaders headers = SourceFileUtil.restTemplateHeader(appKey);
		HttpEntity<String> entity = new HttpEntity<>(null, headers);
		ResponseEntity<APIResponse> responseEntity = restTemplate
				.exchange(url + "?mobile={mobile}", HttpMethod.GET, entity, APIResponse.class, param);
		return responseEntity.getBody();
	}


	@ApiOperation("pc后台管理系统首页验证码校验")
	@GetMapping(value = "/pc/checkCode")
	public APIResponse checkCode(
			@ApiParam(name = "code", value = "用户输入的验证码", required = true)
			@RequestParam String code,
			@ApiParam(name = "mobile", value = "用户手机号", required = true)
			@RequestParam String mobile) throws Exception {
		Map<String, Object> param = new HashMap<>();
		param.put("mobile", mobile);
		param.put("code", code);
		String url = centerProperties.getAuthSystemApi()
				.getCheckCode();
		String appKey = SourceFileUtil.source("authKey.txt");
		HttpHeaders headers = SourceFileUtil.restTemplateHeader(appKey);
		HttpEntity<String> entity = new HttpEntity<>(null, headers);
		ResponseEntity<APIResponse> responseEntity = restTemplate
				.exchange(url + "?mobile={mobile}&code={code}", HttpMethod.GET, entity, APIResponse.class, param);
		return responseEntity.getBody();

	}

	@ApiOperation("pc后台管理系统首页修改密码")
	@GetMapping(value = "/pc/updatePassWord")
	public APIResponse updatePassWord(
			@ApiParam(name = "mobile", value = "用户手机号码", required = true)
			@RequestParam String mobile,
			@ApiParam(name = "pwd", value = "用户设置的新密码", required = true)
			@RequestParam String pwd) throws Exception {
		Map<String, Object> param = new HashMap<>();
		param.put("mobile", mobile);
		param.put("pwd", pwd);
		String url = centerProperties.getAuthSystemApi()
				.getUpdatePassWord();
		String appKey = SourceFileUtil.source("authKey.txt");
		HttpHeaders headers = SourceFileUtil.restTemplateHeader(appKey);
		HttpEntity<String> entity = new HttpEntity<>(null, headers);
		ResponseEntity<APIResponse> responseEntity = restTemplate
				.exchange(url + "?mobile={mobile}&pwd={pwd}", HttpMethod.GET, entity, APIResponse.class, param);
		return responseEntity.getBody();
	}


	@ApiOperation("cityData json返回")
	@PostMapping(value = "/mobile/cityData")
	public APIResponse cityDataJson() throws IOException {
		String jsonString = SourceFileUtil.source("city.data/global_cityData.json");
		JSONArray jsonObject = JSONArray.parseArray(jsonString);
		return APIResponse.success(jsonObject);
	}


}
